myApp.pages.index = {
    init: function () {
        //登录部分
        $$('.login-screen').find('.button').on('click', function () {
            let username = $$('.login-screen').find('input[name="username"]').val();
            let password = $$('.login-screen').find('input[name="password"]').val();
            if (username === 'rean' && password === 'huen') {
                myApp.closeModal('.login-screen');
                myApp.pages.index.initToday();
            } else {
                myApp.alert('Incorrect username or password.')
            }

            //todo 处理登录
        });

        $$('#addMore').on('click', function () {
            $$('#addContainer').append($$('#addTemplate').html())
        });

        $$('#submit').on('click', function () {

            let date = $$('#date').val();
            if (date === '' || date === undefined || date === null) {
                myApp.alert('Please select a DATE !');
                return false;
            }
            let data = {
                date: date,
                finance: []
            };
            let $addContainer = $$('#addContainer li');
            for (let i = 0; i < $addContainer.length; i++) {
                let $that = $$($addContainer[i]);
                let detail = $that.find('.detail').val();
                let amount = $that.find('.amount').val();
                if (detail === null || detail === undefined || detail === '' || detail.length === 0) {
                    myApp.alert('DETAIL CANNOT be empty !');
                    return false;
                }
                if (isNaN(amount) === true || parseFloat(amount) < 0.0) {
                    myApp.alert('AMOUNT allow only a number greater than 0 !');
                    return false;
                }
                data.finance.push({detail: detail, amount: amount})
            }
            myApp.api.createFinance({
                data: data, success: function (result) {
                    myApp.alert('Add Success ! ');
                    myApp.pages.index.renderToday(result);
                }
            })
        });
        $$('#reset').on('click', function () {
            $$('#date').val('');
            $$('#addContainer').html($$('#addTemplate').html())
        });


        //初始化删除
        $$('#todayList').on('deleted', function (e) {
            myApp.api.deleteFinance({
                data: {id: $$(e.target).data('id')}, success: function (data) {
                    myApp.pages.index.renderToday(data);
                }
            })
        });

    },
    initToday: function () {
        myApp.api.getToday({
            success: function (result) {
                myApp.pages.index.renderToday(result);
            }
        })
    },
    renderToday: function (data) {
        let total = 0;
        data.forEach((row) => {
            total += row.amount;
        });
        $$('#todayList').html(myApp.template.render('#todayListTmpl', {todays: data}));
        $$('#todayTotal').html('￥' + total / 100);
        $$('#addContainer').html($$('#addTemplate').html());
        $$('#date').val('');
    }
};